﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class TDItController : Controller
    {
        //
        // GET: /TDIt/

        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSTrinhDoTinHoc
                            from cap in context.DSCapTinHoc
                            where dt.IDCanBo == id && dt.IDTrinhDo == cap.ID
                            select new {dt.ID,dt.STT,dt.NgayCap,dt.IsThongKe,cap.Ten }).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.STT,
                                 customer.Ten,
                                 //customer.IDTrinhDo,
                                 customer.NgayCap,

                                 customer.IsThongKe
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index(int id)
        {
            List<TrinhDoTinHoc> dsqhgd = TrinhDoTinHocModels.GetTrinhDoTinHocTheoCanBo(id);
            ViewBag.TrinhDoTinHoc = dsqhgd;
            CanBo cb = CanBoModels.GetChiTietCanBo(id);
            ChucVu cv = PosModels.GetChiTietDSChucVu((int)cb.ChucVuHienTai);
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(cb.IDPhongBan);
            ViewBag.ChucVu = cv;
            ViewBag.PhongBan = pb;
            ViewBag.ChiTietCanBo = cb;
            return View(dsqhgd);
        }
        public ActionResult Add(int id = 0)
        {            
            ViewBag.Id = id;
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var kq = from cth in context.DSCapTinHoc
                     select cth;
            List<SelectListItem> dscth = new List<SelectListItem>();
            foreach (var item in kq)
            {
                SelectListItem slitem = new SelectListItem();
                slitem.Value = item.ID.ToString();
                slitem.Text = item.Ten;
                dscth.Add(slitem);
            }
            ViewBag.Dscth = dscth;
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            TrinhDoTinHoc object1 = new TrinhDoTinHoc();
            object1.STT = Convert.ToInt32(form["STT"]);
            object1.IDTrinhDo = Convert.ToInt32( form["TrinhDo"]);
            object1.NgayCap = DateTime.Parse(form["NgayCap"],ci.DateTimeFormat);

            bool isChecked = (form["IsThongKe"] != "false");
           object1.IsThongKe = isChecked;

           object1.IDCanBo = id;

           TrinhDoTinHocModels.Add(object1);

           return RedirectToAction("Index", "TDIt", new { id = object1.IDCanBo });
        }
        public ActionResult Update(int id)
        {
            TrinhDoTinHoc listQHGD = TrinhDoTinHocModels.GetChiTietTrinhDoTinHoc(id);
            ViewBag.TrinhDoTinHoc = listQHGD;
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var kq = from cth in context.DSCapTinHoc
                     select cth;
            List<SelectListItem> dscth = new List<SelectListItem>();
            foreach (var item in kq)
            {
                SelectListItem slitem = new SelectListItem();
                slitem.Value = item.ID.ToString();
                slitem.Text = item.Ten;
                if (item.ID == listQHGD.IDTrinhDo)
                {
                    slitem.Selected = true;
                }
                dscth.Add(slitem);
            }
            ViewBag.Dscth = dscth;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            TrinhDoTinHoc object1 = TrinhDoTinHocModels.GetChiTietTrinhDoTinHoc(id);
            object1.STT = Convert.ToInt32(form["STT"]);
            object1.IDTrinhDo = Convert.ToInt32(form["TrinhDo"]);
            object1.NgayCap = DateTime.Parse(form["NgayCap"],ci.DateTimeFormat);

            bool isChecked = (form["IsThongKe"] != "false");
            object1.IsThongKe = isChecked;

            TrinhDoTinHocModels.Update(object1);
            return RedirectToAction("Index", "TDIt", new { id = object1.IDCanBo });
        }
        public ActionResult Delete(int id)
        {
            TrinhDoTinHoc s = TrinhDoTinHocModels.GetChiTietTrinhDoTinHoc(id);
            TrinhDoTinHocModels.Delete(id);
            return RedirectToAction("Index", "TDIt", new { id = s.IDCanBo });
        }

    }
}
